package com.pignest.hotel.ui.reception;

import java.util.Date;
import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;


import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.beans.TbLiveGoodsBorrow;
import com.pignest.hotel.definition.InComeItemType;
import com.pignest.hotel.definition.RoomLiveStatus;
import com.pignest.hotel.definition.RoomUseStatus;
import com.pignest.hotel.service.ReceptionService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.InComeUtil;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.MoneyUtil;
import com.pignest.hotel.util.UIUtils;

public class CheckServiceWindow extends Window implements EventListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 227391272365009917L;

	private ReceptionService receptionService;
	private Room room;
	public Living living;
	private boolean success;//业务是否成功完成
	
	private Label roomName;	//房间名
	private Label roomType;//房间类型
	private Label roomStatus;//房间状态
	//private Textbox livingTypeTextbox;
	private Textbox relativeRoomTextbox;		//关联的房间号, 团队, 联房
	private Textbox customNameTextbox;//主客姓名
	private Textbox sexListbox;//性别
	private Textbox phoneTextbox;//联系电话



    private Textbox memberLevelText;

	private Textbox adjustPriceTextbox;
	private Textbox liveTime;
	private Label storage;
	private Label used;
	private Label balance;
	private Label payed;
    private Button backBtn;
  //  private Button payGoodsbtn;
	private Textbox remarkTextbox;
	
	//private Listbox payReasionListbox;
	private Listbox checkServiceListbox;
	private Listbox goodsBorrowlistbox;
	public CheckServiceWindow() {
		receptionService = Services.getReceptionService();
		addEventListener("onCreate", this);
	}
	@Override
	public void onEvent(Event event) throws Exception {
		// TODO Auto-generated method stub
		if ("onCreate".equals(event.getName())) {
			roomName = (Label) getFellow("roomName");
			roomType =(Label)getFellow("roomType");
			roomStatus=(Label)getFellow("roomStatus");
			//livingTypeTextbox = (Textbox) getFellow("livingType");
			relativeRoomTextbox = (Textbox) getFellow("relativeRoom");
			customNameTextbox = (Textbox) getFellow("customName");
			sexListbox = (Textbox) getFellow("gender");
			phoneTextbox = (Textbox) getFellow("phone");
			//payReasionListbox=(Listbox)getFellow("payReasionListbox");
	
			memberLevelText =(Textbox) getFellow("memberLevel");

			adjustPriceTextbox = (Textbox) getFellow("adjustPrice");
			liveTime = (Textbox) getFellow("liveTime");

			remarkTextbox = (Textbox)getFellow("remark");
			storage =(Label)getFellow("storage");
			used =(Label)getFellow("used");
			balance =(Label)getFellow("balance");
			payed =(Label)getFellow("payed");
			backBtn=(Button)getFellow("backBtn");
			//payGoodsbtn=(Button)getFellow("payGoodsbtn");
			checkServiceListbox=(Listbox)getFellow("checkServiceListbox");
			goodsBorrowlistbox=(Listbox)getFellow("goodsBorrowlistbox");

			Living l= (Living)receptionService.getLiving(room);
			this.living=l;
			
			if(l==null){
				return ;
			}
			init();
		}
	}
	public void init()
	{
		initBaseLivingInfo(living);
		calculate(living);
		queryBorrowGoods(living.getId());
		queryAccounts();
		/***设置物品归还*/
		isGoodBorrow();
	}
	public void initBaseLivingInfo(Living l)
	{
        roomName.setValue(room.getName());
        roomType.setValue(room.getType().getName());
        roomStatus.setValue(RoomLiveStatus.valueof(room.getLiveStatus()).getName()+RoomUseStatus.valueof(room.getUseStatus()).getName());
        remarkTextbox.setValue(l.getRemark());
		String memberLevel="";
		if(l!=null&&l.getMember()!=null){
		   memberLevel=l.getMember().getMemberLevel().getMemberlevel();
           memberLevelText.setValue(memberLevel);
		}
		//livingTypeTextbox.setValue(l.getLiveType());
		customNameTextbox.setValue(l.getCustormName());
		sexListbox.setValue(l.getSex());
		phoneTextbox.setValue(l.getPhone());
        adjustPriceTextbox.setValue(Utils.toPrice(l.getAdjustPrice()));
		liveTime.setValue(DateUtil.toString(l.getLiveTime(), DateUtil.HOTEL_DEFAULT_FORMAT));//入住时间	
	}
	public void calculate(Living l){
		InComeUtil.countInComeNoAdd(l, storage, used, payed, balance);
	}
	/**
	 * 操作完成相关业务后调用
	 * */
	public void flush(){
		//calculate(this.living.getId(),this.living.getStorage());
		calculate(living);
		queryAccounts();
		queryBorrowGoods(this.living.getId());
	}
	public void queryAccounts(){
		this.checkServiceListbox.getItems().clear();
		StringBuffer hql=new StringBuffer();
	       if(this.living!=null)
	       {
			  hql.append(" from TbAccounts b where b.living.id='"+this.living.getId()+"'");
			  hql.append(" and b.type!='").append(InComeItemType.YJ.name()).append("'");
	       }  
		    List <TbAccounts> atList=receptionService.find(hql.toString());
		    for(TbAccounts s:atList){
		    	Listitem listitem = new Listitem();
		    	listitem.setParent(checkServiceListbox);
		    	listitem.setValue(s.getId());
		    	listitem.appendChild(new Listcell(s.getName()));
		    	listitem.appendChild(new Listcell(Utils.toPrice(s.getPrice())));
		    	/*Integer i =s.getCounts();
		    	if(i!=null)*/
		    		listitem.appendChild(new Listcell(String.valueOf(s.getCounts())));
		    	/*else
		    		listitem.appendChild(new Listcell(""));*/
		    	
		    	listitem.appendChild(new Listcell(Utils.toPrice(s.getPrice2())));
		    	listitem.appendChild(new Listcell(s.getEndstate()));
		    	listitem.appendChild(new Listcell(s.getState2()));
		    	listitem.appendChild(new Listcell(s.getOpUser()));
		    	listitem.appendChild(new Listcell(s.getOpdate()));
		    }
	}
	public void queryBorrowGoods(String livingId ){
		goodsBorrowlistbox.getItems().clear();
		StringBuffer hql=new StringBuffer();
		if(this.living!=null){
			 hql.append(" from TbLiveGoodsBorrow b where b.living.id='"+living.getId()+"'");
			 List <TbLiveGoodsBorrow> goodsBorrowList=receptionService.find(hql.toString());
			    for(final TbLiveGoodsBorrow s:goodsBorrowList){
			    	Listitem listitem = new Listitem();
			    	listitem.setValue(s.getId());
			    	listitem.setParent(goodsBorrowlistbox);
			    	listitem.appendChild(new Listcell(s.getTbGoods().getName()));
			    	listitem.appendChild(new Listcell(DateUtil.toString(s.getTime(), DateUtil.HOTEL_DEFAULT_FORMAT)));
			    	listitem.appendChild(new Listcell(DateUtil.toString(s.getBacktime(),DateUtil.HOTEL_DEFAULT_FORMAT)));
			    	final Date backtime =s.getBacktime();
			    	Integer i=s.getCount();
			    	if(i!=null)
			    	   listitem.appendChild(new Listcell(String.valueOf(s.getCount())));
			    	else
			    		listitem.appendChild(new Listcell(""));
			    	listitem.appendChild(new Listcell(MoneyUtil.toString(s.getTbGoods().getPrice())));
			    	listitem.addEventListener(Events.ON_CLICK,  new EventListener(){
				   public void onEvent(Event event) throws Exception {
					   TbLiveGoodsBorrow gb =receptionService.get(TbLiveGoodsBorrow.class, s.getId());
					   chekcButonGB(gb);
				   }
			   });
			    
			    }
		}
	   
	}
	public void chekcButonGB(TbLiveGoodsBorrow gb)
	{
		if(gb.getBacktime()==null)
		{
			backBtn.setDisabled(false);
			//payGoodsbtn.setDisabled(false);
		}else{
			backBtn.setDisabled(true);
			//payGoodsbtn.setDisabled(true);
		}
	}
	/**
	 * 归还物品借用
	 * 设定归还时间
	 * ***/
   public String checkBack(){
	   try{
			String [] ids =UIUtils.getSeclectListId(goodsBorrowlistbox);
			receptionService.updateBorrowGoodsBackTime(ids);
			queryBorrowGoods(this.living.getId());
			return "";
			}catch(Exception e){
				e.printStackTrace();
				return "异常"+e.getMessage();
			}
   }
	public void setRoom(Room room) {
		this.room = room;
	}

    public void isGoodBorrow(){
    	boolean isback =receptionService.isGoodsBack(this.living);
    	//if(isback)
    	//addGoodBorrowBtn.setLabel("有物品未归还请核实！");
    	//else
    		//addGoodBorrowBtn.setLabel("没有物品未归还");
    	
    }

public String deleteProjects() {
		try {
			receptionService.executeInTransaction(new Runnable() {
				@Override
				public void run() {
					String[] ids = UIUtils.getSeclectListId(checkServiceListbox);
					receptionService.deleteProjects(ids, living);
					InComeUtil.updateLiving(living.getId());
				}
			});
		flush();
			return "";
		} catch (Exception e) {
			e.printStackTrace();
			return "异常";
		}
	}
    public boolean isSuccess() {
		return success;
	}
	/***
	 * 设定照价赔偿
	 * 
	 * **/
	/*public String checkPayGoods(){
		try{
			String [] ids =UIUtils.getSeclectListId(goodsBorrowlistbox);
			String payReason=UIUtils.getSelectedValue(payReasionListbox);
			String re=receptionService.insertProjects(ids, living, payReason);
			if(!"".equals(re)){
				return re;
			}
			flush();
			return "";
		}catch(Exception e){
			e.printStackTrace();
			return "异常"+e.getMessage();
		}
	}*/
	public List<Living> getLiving(){
		if(this.living!=null){
			StringBuffer hql = new StringBuffer();
			hql.append(" from Living b where b.id='"+living.getId()+"'");
		 List <Living> list=receptionService.find(hql.toString());
		 return list;
		}
		return null;
		
	}
}
